package com.yida.gwj.vote.outside.service.impl;

import com.yida.common.core.exception.ServiceException;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.datasource.annotation.Slave;
import com.yida.gwj.common.constant.GwjConstants;
import com.yida.gwj.common.domain.GwjBaseBuild;
import com.yida.gwj.common.domain.GwjBaseCommunity;
import com.yida.gwj.common.domain.GwjBaseHouse;
import com.yida.gwj.common.domain.GwjBaseOwner;
import com.yida.gwj.common.service.IGwjBaseBuildService;
import com.yida.gwj.common.service.IGwjBaseCommunityService;
import com.yida.gwj.common.service.IGwjBaseHouseService;
import com.yida.gwj.common.service.IGwjBaseOwnerService;
import com.yida.gwj.common.utils.GwjConvertUtil;
import com.yida.gwj.vote.outside.domain.dto.GwjBaseBuildHouseOwnerDTO;
import com.yida.gwj.vote.outside.service.IGwjBaseDataService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 投票楼栋Service业务层处理
 *
 * @author lqq
 * @date 2023-03-22
 */
@Service
@Slave
public class GwjBaseDataServiceImpl implements IGwjBaseDataService {

    @Resource
    private IGwjBaseBuildService gwjBaseBuildService;

    @Resource
    private IGwjBaseHouseService gwjBaseHouseService;

    @Resource
    private IGwjBaseOwnerService gwjBaseOwnerService;

    @Resource
    private IGwjBaseCommunityService gwjBaseCommunityService;

    /**
     * 根据楼栋ID查询所有数据（楼栋、房屋、业主）
     *
     * @param buildIds 楼栋ID集合
     * @param areaId   地区ID
     * @return /
     */
    @Override
    public GwjBaseBuildHouseOwnerDTO getAllDataByBuildIds(Long[] buildIds, Long areaId) {
        //从基础库查询楼栋
        List<GwjBaseBuild> baseBuilds = gwjBaseBuildService.selectGwjBaseBuildByBuildIdList(buildIds);
        //剔除公用户
        baseBuilds.removeIf(b -> GwjConstants.YES.equals(b.getPublicFlag()));
        if (StringUtils.isEmpty(baseBuilds)) {
            throw new ServiceException("查无基础楼栋数据");
        }
        //查询基础房屋
        String[] buildCodes = baseBuilds.stream().map(GwjBaseBuild::getBuildCode).toArray(String[]::new);
        List<GwjBaseHouse> baseHouses = gwjBaseHouseService.selectGwjBaseListByBuildCodesAndAreaId(buildCodes, areaId);
        //剔除公用户
        baseHouses.removeIf(h -> GwjConstants.YES.equals(h.getPublicFlag()));
        if (StringUtils.isEmpty(baseHouses)) {
            throw new ServiceException("查无基础房屋数据");
        }
        //查询基础业主
        List<GwjBaseOwner> baseOwners = new ArrayList<>();
        List<String> houseCodes = baseHouses.stream().map(GwjBaseHouse::getHouseCode).collect(Collectors.toList());
        //分段查询
        List<List<String>> codeList = GwjConvertUtil.partition(houseCodes, 100);
        for (List<String> code : codeList) {
            List<GwjBaseOwner> owners = gwjBaseOwnerService.selectGwjBaseListByGwjBaseHouseCodes(code.toArray(new String[0]), areaId);
            if (StringUtils.isNotEmpty(owners)) {
                baseOwners.addAll(owners);
            }
        }
        if (StringUtils.isEmpty(baseOwners)) {
            throw new ServiceException("查无基础业主数据");
        }
        baseOwners.forEach(o -> {
            if (StringUtils.isEmpty(o.getOwnerPhone())) {
                throw new ServiceException("请完善业主信息");
            }
        });
        GwjBaseBuildHouseOwnerDTO dto = new GwjBaseBuildHouseOwnerDTO();
        dto.setBaseBuilds(baseBuilds);
        dto.setBaseHouses(baseHouses);
        dto.setBaseOwners(baseOwners);
        return dto;
    }

    /**
     * 查询基础数据（小区信息）
     *
     * @param id 基础数据（小区信息）主键
     * @return 基础数据（小区信息）
     */
    @Override
    public GwjBaseCommunity selectGwjBaseCommunityByGwjBaseCommunityId(Long id) {
        return gwjBaseCommunityService.selectGwjBaseCommunityByGwjBaseCommunityId(id);
    }
}
